package com.wedu.modules.sys.controller;


import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.common.validator.ValidatorUtils;
import com.wedu.modules.sys.entity.GameEntity;
import com.wedu.modules.sys.entity.SysDictEntity;
import com.wedu.modules.sys.service.GameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("game")
public class GameController extends AbstractController {
	@Autowired
	private GameService gameService;

	/**
	 * 查询分页列表数据
	 * @param params
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("game:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = gameService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 查询字典列表数据
	 */
	@GetMapping("/dict/list")
	@RequiresPermissions("game:list")
	public R childList() {
		Map<String, Object> dictMap = gameService.queryChildPage();

		return R.ok().put("dictMap", dictMap);
	}

	/**
	 * 按照id查询进行修改
	 * @param id
	 * @return
	 */
	@GetMapping("/info/{id}")
	@RequiresPermissions("game:info")
	public R info(@PathVariable("id") Long id){
		GameEntity game = gameService.getById(id);

		return R.ok().put("game", game);
	}

	/**
	 * 保存游戏
	 * @param game
	 * @return
	 */
	@PostMapping("/save")
	@RequiresPermissions("game:save")
	public R save(@RequestBody GameEntity game){
		ValidatorUtils.validateEntity(game);
		game.setCreateBy(getUserId());
		gameService.saveGame(game);
		return R.ok();
	}

	/**
	 * 修改游戏
	 * @param game
	 * @return
	 */
	@PostMapping("/update")
	@RequiresPermissions("game:update")
	public R update(@RequestBody GameEntity game){
		ValidatorUtils.validateEntity(game);
		game.setUpdateBy(getUserId());
		gameService.update(game);
		return R.ok();
	}

	/**
	 * 按照id删除
	 * @param ids
	 * @return
	 */
	@PostMapping("/delete")
	@RequiresPermissions("game:delete")
	public R delete(@RequestBody Long[] ids){
		gameService.deleteBatch(ids);
		return R.ok();
	}
}
